#!/usr/bin/perl
# widetabfix.pl

use strict; use warnings;

open(OUT, ">tab1.csv");

#foreach my $file (<$ARGV[0]>) { 	# Windows
foreach my $file (@ARGV) {			# Mac
	
	open(IN,"<$file");
	while (my $line = <IN>) {
		if ($line =~ m/,"[A-Z][^"]+"(?=(,"[0-9]+,[0-9]|,"[A-Z]|,"-",|,NA))/) { # Find all lines with an acronym followed by either a vote share (with a decimal comma), another acronym, or a dash
			$line =~ s/(,"(?!CMUS)[A-Z][^"]+")(?=(,"[0-9]+,[0-9]|,"[A-Z]|,"-",|,NA))/$1,"-"/g;  # Replace these acronyms with themselves *plus* a comma-delimited dash (the CMUS is a kludge for the Czech case)
			$line =~ s/(,NA)+$//g; # strip trailing NAs
			print OUT "$line";
		}
		else {print OUT "$line"}
	}
	close IN;
}
close OUT;

